package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.po.RolePo;
import com.dragee.modules.work.entity.TeamEntity;
import com.dragee.modules.work.entity.TeamRefereeEntity;
import com.dragee.modules.work.service.TeamRefereeService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 裁判
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-19 09:42:02
 */
@RestController
@RequestMapping("work/teamreferee")
@Api(tags = "work - 裁判")
public class TeamRefereeController {
    @Autowired
    private TeamRefereeService teamRefereeService;
    @Autowired
    private TeamService teamService;




    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("本队的随队裁判列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        RolePo role = loginUser.getRole();
        if (role.getRoleKey().equals("duiwu")) {
            // 队伍登录
            TeamEntity team = teamService.getTeamByUserId(loginUser.getUserId().toString());
            params.put("teamKey", team.getId());
        }
        PageUtils page = teamRefereeService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("裁判信息")
    public R info(@PathVariable("id") Long id){
		TeamRefereeEntity teamReferee = teamRefereeService.getById(id);
        return R.ok().put("data", teamReferee);
    }


    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改裁判")
    public R saveOrUpdate(@RequestBody TeamRefereeEntity teamReferee){
        TeamEntity team = teamService.getTeamByUserId(LoginUserUtil.getLoginUserId().toString());
        teamReferee.setTeamKey(team.getId().toString());
        teamReferee.setTeamId(team.getCode());
        teamRefereeService.saveOrUpdate(teamReferee);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("裁判删除")
    public R delete( String id){
		teamRefereeService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("裁判批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> teamRefereeService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
